# 3.新建“exerc3”Python文件完成作业:使用jieba对“三国演义”中的词频进行统计并输出
import jieba


def main():
    n = 100  # 表示查看前几的字频
    s = dakai('三国.txt')
    s = tjgs(s)
    s = jg(s)
    s = sc(s, n)
    for i in s:
        print(i)


def dakai(path):
    s = open('三国.txt', 'r', encoding='utf-8')
    return s.read()


def tjgs(str):
    list1 = []
    list1 = jieba.lcut(str)
    return list1


def jg(s):
    dict1 = {}
    for i in s:
        if len(i)>1:
            dict1[i] = dict1.get(i, 0) + 1
    return dict1


def sc(s, n):
    list1 = []
    list2 = []
    for i in s.items():
        list1.append(i)
    list1.sort(key=lambda x: x[1], reverse=True)
    for i in range(n):
        list2.append(list1[i])
    return list2


main()
